package com.wql.service.impl;

import com.aaa.entity.BBusinessInfo;
import com.aaa.entity.GGoods;
import com.aaa.entity.OStock;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wql.mapper.BBusinessInfoMapper;
import com.wql.mapper.OStockMapper;
import com.wql.service.GGoodsService;
import com.wql.mapper.GGoodsMapper;
import com.wql.utils.WebUtils;
import com.wql.vo.SearchFormData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
* @author 咕噜咕噜
* @description 针对表【g_goods(商品表)】的数据库操作Service实现
* @createDate 2023-03-04 15:43:11
*/
@Service
public class GGoodsServiceImpl extends ServiceImpl<GGoodsMapper, GGoods>
    implements GGoodsService{

    @Autowired
    private GGoodsMapper gGoodsMapper;

    @Autowired
    private OStockMapper oStockMapper;

    @Autowired
    private BBusinessInfoMapper bBusinessInfoMapper;
    @Override
    public Result selectAllGoodsInfoByBusinessId(Integer currentPage, Integer pageSize, SearchFormData searchFormData) {
        HttpServletRequest request = WebUtils.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username = tokenChaim.get("username");
        String telephone = username.toString();
        searchFormData.setTelephone(telephone);
        PageHelper.startPage(currentPage,pageSize);
        List<GGoods> gGoods = gGoodsMapper.queryByBusinessId(searchFormData);
        PageInfo<GGoods> info = new PageInfo<>(gGoods);
        return new Result(2000,"查询成功",info) ;
    }

    @Override
    public Result changeGoods(GGoods gGoods) {
        gGoodsMapper.updateById(gGoods);
        oStockMapper.changeStock(gGoods.getId(),gGoods.getoStock().getTotalNum());
        return new Result(2000,"成功");
    }

    @Override
    public Result addGoods(GGoods gGoods) {
        System.out.println("gGoods = " + gGoods);
        HttpServletRequest request = WebUtils.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username = tokenChaim.get("username");
        String s = username.toString();
        QueryWrapper<BBusinessInfo> wrapper = new QueryWrapper<>();
        wrapper.eq( "telephone",username);

        BBusinessInfo bBusinessInfo = bBusinessInfoMapper.selectOne(wrapper);
        gGoods.setBid(bBusinessInfo.getId());

        gGoods.setState(2);
        gGoods.setStatus(1);
//        QueryWrapper<GGoods> wrapper2 = new QueryWrapper<>();
//        wrapper2.eq("gname",gGoods.getGname());
//        wrapper2.eq("bid",bBusinessInfo.getId());
//        List<GGoods> gGoods2 = gGoodsMapper.selectList(wrapper2);
        List<GGoods> gGoods2 = gGoodsMapper.selectGood(gGoods.getGname(), bBusinessInfo.getId());
        if (gGoods2.isEmpty()){
            int insert = gGoodsMapper.insert(gGoods);
            QueryWrapper<GGoods> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("gname",gGoods.getGname());
            GGoods gGoods1 = gGoodsMapper.selectOne(wrapper1);

            OStock oStock = new OStock();
            oStock.setGoodsId(gGoods1.getId());
            oStock.setTotalNum(gGoods.getTotalnum());
            BigDecimal price = new BigDecimal(gGoods.getPrice());
            oStock.setPrice(price);
            oStock.setResidue(gGoods.getTotalnum().toString());
            oStock.setSellNum("0");

            oStockMapper.insert(oStock);
            System.out.println("oStock = " + oStock);
            return new Result(2000,"成功");
        }else {
            return  new Result(5000,"商品已经存在,请勿重复添加");
        }

    }
    @Override
    public Result delete(Integer id) {
        gGoodsMapper.deleteById(id);
        return new Result(2000,"删除成功");
    }

    @Override
    public Result updateStatus(GGoods gGoods) {
        gGoodsMapper.updateById(gGoods);
        return new Result(2000,"状态更新成功");
    }
}




